package com.zllkj.mm.module.freetime

import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.adapter.FreeTimeWaitToConfirmListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.model.entity.*
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.home.fragments.MyCollectionFragment
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import java.util.HashMap

class WaitToConfirmFreeTimeOrderFragment:BaseFragment() {
    var datas = ArrayList<WaitToConfirmOrderVoRecord>()
    lateinit var adapter : FreeTimeWaitToConfirmListAdapter
    var currentPage = 1


    override fun getLayoutId(): Int {
        return R.layout.fragment_mycollection
    }

    override fun loadData() {
        super.loadData()
        adapter = FreeTimeWaitToConfirmListAdapter(R.layout.item_wait_to_confirm_freetime_list,datas)
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter


        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            addFragment(WaitToConfirmFreeTimeOrderDetailFragment(datas[i]))
        }


        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)
    }

    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().freeTimeApi.getToConfirmList((HashMap<String, Any>().apply {
            this["goodsName"] = ""
            this["currentPage"] = currentPage
            this["pageSize"] = 20
        })).compose<WaitToConfirmOrderVo>(RxHelper.defaultHandleResult<WaitToConfirmOrderVo>()).subscribe(object : ProgressSubscriber<WaitToConfirmOrderVo>(requireContext()){
            override fun onSucc(t: WaitToConfirmOrderVo?) {
                    t?.records?.map {
                        datas.add(it)
                        adapter.notifyDataSetChanged()
                    }


                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                    mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

    companion object {
        @JvmStatic
        fun newInstance(): MyCollectionFragment {
            return MyCollectionFragment()
        }
    }
}